﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.Lib;
using CCH.DAO;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            SessionObject sobj = new SessionObject();
            this.lab_name.Text = sobj.SessionUserName;
        }


    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sfu_no">子代編號</param>
    public void Create_menu(int sfu_no)
    {

        sysfunctionDAO dao = new sysfunctionDAO();

        //網頁導覽標題
        if (sfu_no != 0)
        {
            this.lab_title.Text = dao.Get_titleName(sfu_no);
        }
        else
        {
            this.lab_title.Text = "";
        }
        

        //取得父代
        int sfu_parent = 0;
        if (Session["User_SfuParentNO"] != null)
        {
            sfu_parent = Convert.ToInt32(Session["User_SfuParentNO"]);
        }
        else
        {
            if (sfu_no != 0)
            {
                sfu_parent = dao.Get_sfu_parentNO(sfu_no);
            }
        }

        //取得第一層資料
        var lv1Data = dao.Get_sysfunction_parent();

        //第一層清空
        this.div_top_center.Controls.Clear();
        foreach (var d in lv1Data)
        {
            ImageButton button = new ImageButton();

            button.ID = "button_" + d.sfu_no;
            button.ToolTip = d.sfu_name;
            button.CommandArgument = d.sfu_no.ToString();
            button.ClientIDMode = ClientIDMode.Static;

            if (d.sfu_no == sfu_parent)
            {
                button.ImageUrl = ResolveUrl("~/image/menu/top_l_m_r/" + d.sfu_overpic);
            }
            else
            {
                button.ImageUrl = ResolveUrl("~/image/menu/top_l_m_r/" + d.sfu_pic);
                string over = ResolveUrl("~/image/menu/top_l_m_r/" + d.sfu_overpic);
                button.Attributes.Add("onmouseout", "MM_swapImgRestore()");
                button.Attributes.Add("onmouseover", "MM_swapImage('" + button.ID + "','','" + over + "',1)");
            }

            button.Click += new ImageClickEventHandler(button_Click);

            this.div_top_center.Controls.Add(button);
        }

        //取得第二層資料
        var lv2Data = dao.Get_sysfunction(sfu_parent);

        this.div_top_main1_l.Controls.Clear();
        foreach (var d in lv2Data)
        {
            HyperLink link = new HyperLink();

            link.Text = d.sfu_name;
            link.ToolTip = d.sfu_name;
            link.ID = "Link_" + d.sfu_no;
            link.NavigateUrl = "~/" + d.sfu_path;

            this.div_top_main1_l.Controls.Add(link);
        }

    }

    /// <summary>
    /// 點選第一層功能,動態產生第二層
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void button_Click(object sender, EventArgs e)
    {
        //父代
        int sfu_parent = int.Parse(((ImageButton)sender).CommandArgument);
        Session["User_SfuParentNO"] = sfu_parent.ToString();

        //第一個子代
        var data = new sysfunctionDAO().Get_first_sfuNO(sfu_parent);

        if (data != null)
        {
            Response.Redirect(("~/" + data.sfu_path).ResolveUrl());
        }
        else
        {
            //空白頁
            Response.Redirect("~/index.aspx".ResolveUrl());
        }
        
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Page.Header.DataBind();
    }
}
